package com.lwf.core.processor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

public class HandlerPageProcessor implements PageProcessor {

    private Site site;
    private Handler[] handlerList;

    public HandlerPageProcessor() {
    }

    public HandlerPageProcessor(Site site, Handler... handler) {
        this.site = site;
        handlerList = handler;
    }

    public void process(Page page) {
        String url = page.getUrl().get();
        for (Handler handler : handlerList) {
            if (url.matches(handler.getUrlRegex())) {
                handler.process(page);
            }
        }
    }

    public Site getSite() {
        return site;
    }
}
